home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / STREAM.H < prev    next >
C/C++ Source or Header  |  1992-03-05  |  8KB  |  190 lines

  1. /* Copyright (C) 1989, 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* stream.h */
  21. /* Definitions for Ghostscript stream package */
  22. /* Requires stdio.h */
  23.  
  24. /* Note that the stream package works with bytes, not chars. */
  25. /* This is to ensure unsigned representation on all systems. */
  26. /* A stream can only be read or written, not both. */
  27. /* Note also that the read procedure returns an int, */
  28. /* not a char or a byte, so we can use negative values for EOFC and ERRC. */
  29. typedef struct stream_s stream;
  30. typedef struct {
  31.  
  32.         /* Store # available for reading. */
  33.         /* Return 0 if OK, ERRC if error or not implemented. */
  34.     int (*available)(P2(stream *, long *));
  35.  
  36.         /* Set position. */
  37.         /* Return 0 if OK, ERRC if error or not implemented. */
  38.     int (*seek)(P2(stream *, long));
  39.  
  40.         /* Flush buffered data. */
  41.         /* Return 0 if OK, ERRC if error. */
  42.     int (*flush)(P1(stream *));
  43.  
  44.         /* Flush data (if writing) & close stream. */
  45.         /* Return 0 if OK, ERRC if error. */
  46.     int (*close)(P1(stream *));
  47.  
  48.         /* Refill buffer and reset cptr. */
  49.         /* Return ERRC if not implemented; */
  50.         /* otherwise, set end_status appropriately and return 0. */
  51.     int (*read_buf)(P1(stream *));
  52.  
  53.         /* Write buffer, reset cptr. */
  54.         /* Return 0 if OK, ERRC if error or not implemented. */
  55.     int (*write_buf)(P1(stream *));
  56.  
  57. } stream_procs;
  58. /* Structs for specialized streams -- see below. */
  59. struct lzw_decode_table_s;
  60. struct lzw_encode_table_s;
  61. struct stream_s {
  62.     byte *cptr;            /* pointer to last byte */
  63.                     /* read or written */
  64.     byte *endptr;            /* pointer to last byte */
  65.                     /* containing data for reading, */
  66.                     /* or to be filled for writing */
  67.     byte *cbuf;            /* base of buffer */
  68.     uint bsize;            /* size of buffer, 0 if closed */
  69.     uint cbsize;            /* size of buffer */
  70.     char mode;            /* 2 if reading, 1 if writing */
  71. #define s_mode_read 2
  72. #define s_mode_write 1
  73. #define s_is_valid(s) ((s)->mode != 0)
  74. #define s_is_reading(s) ((s)->mode == s_mode_read)
  75. #define s_is_writing(s) ((s)->mode == s_mode_write)
  76.     int end_status;            /* EOFC if at EOF when buffer */
  77.                     /* becomes empty, ERRC if error */
  78.     long position;            /* file position of beginning of */
  79.                     /* buffer, -1 means not seekable */
  80.     stream_procs procs;
  81.     int num_format;            /* format for Level 2 */
  82.                     /* encoded number reader */
  83.                     /* (only used locally) */
  84.     /* strm is non-zero iff this is a filter stream. */
  85.     stream *strm;            /* the underlying stream */
  86.     int strm_is_temp;        /* if true, strm is a temporary */
  87.                     /* stream and should be freed */
  88.                     /* when this stream is closed */
  89.     /*
  90.      * If were were able to program in a real object-oriented style, 
  91.      * the remaining data would be per-subclass.  It's just too much
  92.      * of a nuisance to do this in C, so we allocate space for the
  93.      * private data of ALL subclasses.
  94.      */
  95.     /* The following are for file streams. */
  96.     FILE *file;            /* file handle for C library */
  97.     int can_close;            /* 0 for stdin/out/err, */
  98.                     /* -1 for line/statementedit, */
  99.                     /* 1 for other files */
  100.     stream *prev, *next;        /* keep track of all files */
  101.     /* The following is used by several decoding filters. */
  102.     int odd;            /* odd digit */
  103.     /* The following are for RunLengthEncode filters. */
  104.     ulong record_size;
  105.     /* The following is for RunLengthEncode and PFBDecode. */
  106.     ulong record_left;        /* bytes left in current record */
  107.     /* The following are for PFBDecode. */
  108.     int record_type;
  109.     /* The following are for eexec streams. */
  110.     ushort cstate;            /* encryption state */
  111.     int binary;            /* true=binary, false=hex */
  112.     /* The following are for LZW encoding/decoding streams. */
  113.     int enhanced;            /* if true, use Aladdin's */
  114.                     /* enhanced compression algorithm */
  115.                     /* (Patent Pending) */
  116.     byte bits;        /* most recent byte of input or */
  117.                 /* current byte of output */
  118.     int bits_left;        /* # of unused low bits in above, [0..7] */
  119.     struct lzw_decode_table_s *decode_table;    /* decoding table */
  120.     struct lzw_encode_table_s *encode_table;    /* encoding table */
  121.     uint next_code;            /* next code to be assigned */
  122.     int code_size;            /* current # of bits per code */
  123.     int prev_code;            /* previous code recognized */
  124.                     /* or assigned */
  125. };
  126.  
  127. /* Stream functions.  Some of these are macros -- beware. */
  128. /* Also note that unlike the C stream library, */
  129. /* ALL stream procedures take the stream as the first argument. */
  130. #define sendbufp(s) ((s)->cptr >= (s)->endptr)    /* not for clients */
  131.  
  132. /* Following are valid for all streams. */
  133. /* flush is a no-op for read streams. */
  134. /* close is NOT a no-op for non-file streams -- */
  135. /* it actively disables them. */
  136. /* The close routine must do a flush if needed. */
  137. #define sseekable(s) ((s)->position >= 0)
  138. #define serrorp(s) ((s)->cptr >= (s)->endptr && (s)->end_status == ERRC)
  139. #define savailable(s,pl) (*(s)->procs.available)(s,pl)
  140. #define sflush(s) (*(s)->procs.flush)(s)
  141. #define sclose(s) (*(s)->procs.close)(s)
  142.  
  143. /* Following are only valid for read streams. */
  144. extern int spgetc(P1(stream *));
  145. #define sgetc(s) (!sendbufp(s) ? *++((s)->cptr) : spgetc(s))
  146. extern uint sgets(P3(stream *, byte *, uint));
  147. extern int sreadhex(P6(stream *, byte *, uint, uint *, int *, int));
  148. extern int sungetc(P2(stream *, byte));    /* ERRC on error, 0 if OK */
  149. #define sputback(s) ((s)->cptr--)
  150. #define seofp(s) (sendbufp(s) && (s)->end_status == EOFC)
  151.  
  152. /* Following are only valid for write streams. */
  153. extern int spputc(P2(stream *, byte));
  154. #define sputc(s,c)\
  155.   (!sendbufp(s) ? ((int)(*++((s)->cptr)=(c))) : spputc((s),(c)))
  156. extern uint sputs(P3(stream *, const byte *, uint));
  157.  
  158. /* Following are only valid for positionable streams. */
  159. #define stell(s) ((s)->cptr + 1 - (s)->cbuf + (s)->position)
  160. #define sseek(s,pos) (*(s)->procs.seek)(s,(long)(pos))
  161.  
  162. /* Following are for high-performance clients. */
  163. /* bufptr points to the next item, bufend points beyond the last item. */
  164. #define sbufptr(s) ((s)->cptr + 1)
  165. #define sbufend(s) ((s)->endptr + 1)
  166. #define ssetbufptr(s,ptr) ((s)->cptr = (ptr) - 1)
  167. #define sbufavailable(s) ((s)->endptr - (s)->cptr)
  168.  
  169. /* We cast EOFC and ERRC to int explicitly, because some compilers */
  170. /* don't do this if the other arm of a conditional is a byte. */
  171. /* Clients know that return values <0 are exceptional. */
  172. #define EOFC ((int)(-1))
  173. #define ERRC ((int)(-2))
  174. /****** ERRC IS NOT RECOGNIZED IN MOST PLACES YET ******/
  175.  
  176. /* Stream creation procedures */
  177. extern    void    sread_string(P3(stream *, byte *, uint)),
  178.         swrite_string(P3(stream *, byte *, uint));
  179. extern    void    sread_file(P4(stream *, FILE *, byte *, uint)),
  180.         swrite_file(P4(stream *, FILE *, byte *, uint));
  181.  
  182. /* Standard stream initialization */
  183. extern    void    s_std_init(P5(stream *, byte *, uint, stream_procs *, int /*mode*/));
  184. /* Standard stream finalization */
  185. extern    void    s_disable(P1(stream *));
  186. /* Generic stream procedures exported for filters */
  187. extern    int    s_std_null(P1(stream *)),
  188.         s_std_noavailable(P2(stream *, long *)),
  189.         s_std_close(P1(stream *));
  190.